package org.mihalovic.PES.GUI;

import javax.swing.JTable;



/**
 * TODO
 */
public class TableRowSelectHelper {
    private JTable table;
    private int CBindex;

    /**
     * @param table
     * @param boolRowIndex zero-based index of column with JCheckBox
     */
    public TableRowSelectHelper(JTable table, int boolRowIndex) {
        this.table = table;
        CBindex = boolRowIndex;
    }


    void adjustRowsCheckStatus() {
        boolean toSelect = !areAllSelectedRowsChecked();

        int[] selectedRowsIndices = table.getSelectedRows();

        for(int i=0; i<selectedRowsIndices.length; i++)
            table.setValueAt(new Boolean(toSelect), selectedRowsIndices[i], CBindex);
    }

    private boolean areAllSelectedRowsChecked() {
        int[] selectedRowsIndices = table.getSelectedRows();

        for(int i=0; i<selectedRowsIndices.length; i++)
            if( !(Boolean)table.getValueAt(selectedRowsIndices[i], CBindex) ) return false;

        return true;
    }

    void setAllRowsChecked(boolean checked) {
        int rows = table.getRowCount();

        for(int i=0; i<rows; i++)
            table.setValueAt(new Boolean(checked), i, CBindex);
    }
}
